//
//  TableViewCell.m
//  iOSCodeLearning
//
//  Created by TFAppleWork-Summer on 2018/4/13.
//  Copyright © 2018年 Ace. All rights reserved.
//

#import "TableViewCell.h"

@interface TableViewCell()

@property (nonatomic, strong) UIButton *applyBtn;

@property (nonatomic, strong) UIButton *jumpBtn;

@end

@implementation TableViewCell


-(instancetype)initWithStyle:(UITableViewCellStyle)style reuseIdentifier:(NSString *)reuseIdentifier{
    if (self = [super initWithStyle:style reuseIdentifier:reuseIdentifier]) {
        [self.contentView addSubview:self.applyBtn];
        [self.contentView addSubview:self.jumpBtn];
        
        [self.applyBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.mas_offset(20);
            make.centerY.equalTo(self.contentView);
        }];
        
        [self.jumpBtn mas_makeConstraints:^(MASConstraintMaker *make) {
            make.left.equalTo(self.applyBtn.mas_right).offset(20);
            make.centerY.equalTo(self.applyBtn);
        }];
        
    }
    return self;
}

+(TableViewCell *)createCellWithTableView:(UITableView *)tableView{
    TableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:NSStringFromClass([self class])];
    if (!cell) {
        cell = [[TableViewCell alloc]initWithStyle:UITableViewCellStyleDefault reuseIdentifier:NSStringFromClass([self class])];
    }
    return cell;
}

-(void)setViewModel:(CellViewModel *)viewModel{
    _viewModel = viewModel;
    [self p_updateSubViews:viewModel];
}

-(void)p_updateSubViews:(CellViewModel *)viewModel{
    if (viewModel.model.index % 2 == 0 ) {
        // 偶数行
        self.backgroundColor = [UIColor orangeColor];
    }else{
        self.backgroundColor = [UIColor grayColor];
    }
    
    [self.applyBtn setTitle:[NSString stringWithFormat:@"申请 %ld",viewModel.model.index] forState:UIControlStateNormal];
    [self.jumpBtn setTitle:[NSString stringWithFormat:@"跳转 %ld",viewModel.model.index] forState:UIControlStateNormal];
}


-(void)applyBtnClick{
    
    [self.viewModel applyHandlercomplete:^(BOOL success) {
        [self.applyBtn setTitle:@"申请11" forState:UIControlStateNormal];
    }];
}

-(void)jumpBtnClick{

    [self.viewModel jumpHandler];
    
}


-(UIButton*)applyBtn{
    if (!_applyBtn) {
        _applyBtn = [[UIButton alloc] init];
        _applyBtn.backgroundColor = [UIColor blueColor];
        [_applyBtn addTarget:self action:@selector(applyBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _applyBtn;
}

-(UIButton *)jumpBtn{
    if (!_jumpBtn ) {
        _jumpBtn = [[UIButton alloc]init];
        _jumpBtn.backgroundColor = [UIColor whiteColor];
        [_jumpBtn setTitleColor:[UIColor blackColor] forState:UIControlStateNormal];
        [_jumpBtn addTarget:self action:@selector(jumpBtnClick) forControlEvents:UIControlEventTouchUpInside];
    }
    return _jumpBtn;
}

-(void)dealloc{
    NSLog(@"-----%@--------",NSStringFromClass([self class]));
}




@end
